package com.example.trafficexam2.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.trafficexam2.bean.Account1;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/12/4/004.
 */

public class TestHelper extends OrmLiteSqliteOpenHelper {
    private static TestHelper instance;
    private Map<String,Dao> daoMap = new HashMap<>();

    public TestHelper(Context context) {
        super(context,"Mydatabase",null,1);
    }
    public static TestHelper getInstance(Context context) {
        if (instance == null){
            synchronized (TestHelper.class){
                if (instance == null){
                    instance = new TestHelper(context);
                }
            }
        }
        return instance;
    }

    public Dao getDao(Class clazz) throws android.database.SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();
        if (daoMap.containsKey(className)){
            dao = daoMap.get(className);
        }
        if (dao == null){
            try {
                dao = super.getDao(clazz);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            daoMap.put(className,dao);
        }
        return dao;
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.clearTable(connectionSource, Account1.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {

    }
}
